package com.example.mypictureviewapp0316;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private RelativeLayout relativeLayout;
    private ImageView imageView;
    private Button button1;
    private Button button2;
    private int[] imageArray;
    private int count=0;
    private GestureDetector myGestureDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=findViewById(R.id.radioGroup);
        relativeLayout=findViewById(R.id.relativeLayout);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButton1:relativeLayout.setBackgroundResource(R.drawable.simg);
                    break;
                    case R.id.radioButton2:relativeLayout.setBackgroundResource(R.drawable.timg);
                    break;
                }
            }
        });

        imageView=findViewById(R.id.imageView);
        button1=findViewById(R.id.button1);
        button2=findViewById(R.id.button2);
        imageArray=new int[]{R.drawable.picture0,R.drawable.picture1,R.drawable.picture2,
                R.drawable.picture3,R.drawable.picture4,R.drawable.picture5,
                R.drawable.picture6,R.drawable.picture7,R.drawable.picture8,
                R.drawable.picture9,R.drawable.picture10,R.drawable.picture11,
                R.drawable.picture12};
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                count=count%13;
                imageView.setImageResource(imageArray[count]);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count--;
                count=count+13;
                count=count%13;
                imageView.setImageResource(imageArray[count]);
            }
        });

        myGestureDetector=new GestureDetector(this, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if((e1.getRawX()-e2.getRawX())>200)
                {
                    count--;
                    count=count+13;
                    count=count%13;
                    imageView.setImageResource(imageArray[count]);
                }
                if((e2.getRawX()-e1.getRawX())>200)
                {
                    count++;
                    count=count%13;
                    imageView.setImageResource(imageArray[count]);
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        }
        );
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return myGestureDetector.onTouchEvent(event);
    }
}
